package com.ruoyi.bole.integral.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.anno.UserCheck;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bole.integral.domain.BoleIntegralOrganization;
import com.ruoyi.bole.integral.service.IBoleIntegralOrganizationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 组织关系Controller
 * 
 * @author wujiale
 * @date 2025-07-04
 */
@RestController
@RequestMapping("/integral/organization")
public class BoleIntegralOrganizationController extends BaseController
{
    @Autowired
    private IBoleIntegralOrganizationService boleIntegralOrganizationService;

    @Log(title = "组织关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/user/{orgId}")
    public AjaxResult removeUser(@PathVariable Long orgId)
    {
        Long[] orgIds = new Long[1];
        orgIds[0]=orgId;
        return toAjax(boleIntegralOrganizationService.deleteBoleIntegralOrganizationByOrgIds(orgIds));
    }
    //获取我的组织数据
    @GetMapping("/user")
    public AjaxResult userOrInfo()
    {
        return AjaxResult.success(boleIntegralOrganizationService.getByuserId());
    }
    @GetMapping("/getByParentAngGroupId")
    public AjaxResult getListAndIsGroupByParentId(@RequestParam("parentId") Long parentId)
    {
        return AjaxResult.success(boleIntegralOrganizationService.getListAndIsGroupByParentId(parentId));
    }


    @GetMapping("/getByParentId")
    public AjaxResult getListByParentId(@RequestParam("parentId") Long parentId)
    {
        return AjaxResult.success(boleIntegralOrganizationService.getListByParentId(parentId));
    }
    //分配可分配积分
//    @PreAuthorize("hasAuthority('regimental')")
//    @PreAuthorize("hasRole('regimental')")role被加上了默认前缀ROLE_
    @PreAuthorize("@ss.hasPermi('integral:organization:edit')")
    @PostMapping("/distrubutePointsByOgId")
    @UserCheck
    public AjaxResult distrubutePointsByOgId(
          @RequestParam  Long userId,
          @RequestParam    Long orgId,
          @RequestParam  Long points,@RequestParam Long bOrgId
    )
    {
        return toAjax(boleIntegralOrganizationService.distrubutePointsByOgId(orgId, points,bOrgId));
    }

    //分配可用积分
//    @PreAuthorize("hasRole('regimental')")
    @PostMapping("/distrubuteAllocatedIntegral")
    @UserCheck
    public AjaxResult distrubuteAllocatedIntegral(
            @RequestParam Long userId,
            @RequestParam   Long toUserId,
            @RequestParam  Long orgId,
            @RequestParam Long points,@RequestParam Long bOrgId,
            @RequestBody Map<String, String> remark
    )
    {
//        System.out.println(remark.get("remark"));
//        return toAjax(1);
        return toAjax(boleIntegralOrganizationService.distrubuteAllocatedIntegralByOrgId(orgId, points,bOrgId,
                toUserId,remark.get("remark")));
    }

    @Log(title = "组织关系", businessType = BusinessType.INSERT)
    @PostMapping("/batchAddLeaders") // 新增批量添加接口
    public AjaxResult batchAddLeaders(
            @RequestParam Long orgId, // 组织ID
            @RequestBody List<Long> userIds // 用户ID列表
    ) {
        // 调用批量插入服务方法
        int count = boleIntegralOrganizationService.batchInsertLeaders(orgId, userIds);
        return toAjax(count);
    }
    @Log(title = "组织关系", businessType = BusinessType.INSERT)
    @PostMapping("/addMembers") // 新增批量添加接口
    public AjaxResult addMembers(
            @RequestParam Long orgId, // 组织ID
            @RequestBody List<Long> userIds // 用户ID列表
    ) {
        // 调用批量插入服务方法
        int count = boleIntegralOrganizationService.addMembers(orgId, userIds);
        return toAjax(count);
    }
    /**
     * 查询组织关系列表
     */
    @PreAuthorize("@ss.hasPermi('integral:organization:list')")
    @GetMapping("/list")
    public TableDataInfo list(BoleIntegralOrganization boleIntegralOrganization)
    {
        startPage();
        List<BoleIntegralOrganization> list = boleIntegralOrganizationService.selectBoleIntegralOrganizationList(boleIntegralOrganization);
        return getDataTable(list);
    }

    /**
     * 导出组织关系列表
     */
    @PreAuthorize("@ss.hasPermi('integral:organization:export')")
    @Log(title = "组织关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BoleIntegralOrganization boleIntegralOrganization)
    {
        List<BoleIntegralOrganization> list = boleIntegralOrganizationService.selectBoleIntegralOrganizationList(boleIntegralOrganization);
        ExcelUtil<BoleIntegralOrganization> util = new ExcelUtil<BoleIntegralOrganization>(BoleIntegralOrganization.class);
        util.exportExcel(response, list, "组织关系数据");
    }

    /**
     * 获取组织关系详细信息
     */
    @PreAuthorize("@ss.hasPermi('integral:organization:query')")
    @GetMapping(value = "/{orgId}")
    public AjaxResult getInfo(@PathVariable("orgId") Long orgId)
    {
        return success(boleIntegralOrganizationService.selectBoleIntegralOrganizationByOrgId(orgId));
    }

    /**
     * 新增组织关系
     */
    @PreAuthorize("@ss.hasPermi('integral:organization:add')")
    @Log(title = "组织关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BoleIntegralOrganization boleIntegralOrganization)
    {
        return toAjax(boleIntegralOrganizationService.insertBoleIntegralOrganization(boleIntegralOrganization));
    }

    /**
     * 修改组织关系
     */
    @PreAuthorize("@ss.hasPermi('integral:organization:edit')")
    @Log(title = "组织关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BoleIntegralOrganization boleIntegralOrganization)
    {
        return toAjax(boleIntegralOrganizationService.updateBoleIntegralOrganization(boleIntegralOrganization));
    }

    /**
     * 删除组织关系
     */
    @PreAuthorize("@ss.hasPermi('integral:organization:remove')")
    @Log(title = "组织关系", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orgIds}")
    public AjaxResult remove(@PathVariable Long[] orgIds)
    {
        return toAjax(boleIntegralOrganizationService.deleteBoleIntegralOrganizationByOrgIds(orgIds));
    }
}
